<?php
class socketUser extends PacketStream {
	public $user;

	public $x;
	public $y;
	public $dx;
	public $dy;

	public $state = 0;

	public $server;

	public $platform ; // на какой платформе находитмя

	const STATE_NORMAL = 0;
	const STATE_JUMP = 1;
	const STATE_SPECTATOR = 2;

	const DIRECTION_LEFT = -1;
	const DIRECTION_RIGHT = 1;

	const PLATFORM_LEFT = 1;
	const PLATFORM_RIGHT = 2;

	public $initialized = false;

	public function onAccept() {
		$this->send(file_get_contents(dirname(__FILE__) . '/../../crossdomain.xml') . "\0");
		$this->initialized = false;
	}



	public function onClose() {
		$query = 'UPDATE `rooms` SET `user_count`=`user_count`-1 WHERE `id`='.$this->server->server_id;
		_database::query($query);
		Log::notice($query);
		Log::notice('client closed');
		return true;
	}

	public function onPacketReceived($packet) {

		Log::debug('request',var_export($packet,true));
		if(is_numeric($packet))
		{
			$this->init($packet);
			return true;
		}
		
		if(!$this->initialized) {
		// надо авторизовать!
			$r = $this->auth($packet);
			if(!$r) {
				$this->sendPacket($this->add_error('auth'));
				return false;
			}
			else {
				$this->init($r);
				return true;
			}
		}

		if(!isset($packet['action'])) {
			$packet['action'] = 'hack';
		}
		switch($packet['action']) {
			case 'ping':
				$this->sendPacket(array("server_time"=>time()));
				break;
			case 'left':
				$this->move(self::DIRECTION_LEFT);
				break;
			case 'right':
				$this->move(self::DIRECTION_RIGHT);
				break;
			case 'jump':
				$this->jump();
				break;
			case 'stopX':
				$this->stopX();
				break;
			default:
				$this->sendPacket($this->add_error('action_illegal'));
				break;
		}
	}

	// говорим всем юзерам
	public function sendToAll($data)
	{
		$this->server->sendToAll($data);
	}

	public function add_error($str,$n=0) {
		$error = array('error'=>$str,'error_number'=>(int)$n);
		return $error;
	}

	public function jump() {
		$this->state = self::STATE_JUMP;
		$this->dy = conf::$jump_implulse;
		$this->platform = 0;
	}

	public function move_start($direction) {
		$this->dx = $direction * conf::$speed_normal;
	}

	public function move($interval) {
		switch ($this->state) {
			case self::STATE_JUMP:
				$this->x = $this->x + $this->dx * conf::$speed_jump;
				$this->dy += conf::$gravity;
				$this->y = $this->y + $this->dy;
				break;
			case self::STATE_NORMAL:
				if($this->dx)
					$this->x = $this->x + $this->dx * conf::$speed_normal;
				break;
		}
	}

	// свалился с платформы
	public function stumble() {
		$this->platform = 0;
		$this->state = self::STATE_JUMP;
	}

	// приземление
	public function Land($platform) {
		$this->state = self::STATE_NORMAL;
		$this->platform = $platform;
		$this->dy = 0;
		return true;
	}

	public function stopX() {
		$this->dx = 0;
	}

	// упал, стал спектатором
	public function fall() {
		if($this->state != self::STATE_SPECTATOR) {
			$this->platform = 0;
			$this->dx = 0;
			$this->dy = 0;
			$this->state = self::STATE_SPECTATOR;
			$this->sendPacket(array('fall'));
		}
	}


	public function auth($array) {
		if(!$array['user_id'] || !$array['secret']) return isset($array['user_id'])?(int)$array['user_id']:rand(10000, 9999999);
		Log::notice('login',var_export($array,1));
		$md5 = md5(conf::$api_id.'_'.$array['user_id'].'_'.conf::$api_secret);
		if($array['secret'] == $md5) {
			Log::notice('login success');
			return $array['user_id'];
		}
		Log::notice('login fail');
		return false;
	}

	function  __construct($sock,$server) {
		parent::__construct($sock);
		$this->server = $server;
	}

	public function init($user_id) {
		$this->user = new User($user_id);
		if($this->user->getProfile()) {
			Log::notice('User inited',$user_id);
			$query = 'UPDATE `rooms` SET `user_count`=`user_count`+1 WHERE `id`='.$this->server->server_id;
			_database::query($query);
			$this->initialized = true;
			return true;
		}
		Log::notice('Cant init User');
		$this->sendPacket($this->add_error('Cant init user'.$user_id));
		$this->sendToAll(array('type'=>'login','user_id'=>$user_id));
		return false;
	}
}